import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Home from "@/views/Home";
import Board from "@/views/Board";
import Card from "@/views/Card";
import Register from "@/views/Register";
import Login from "@/views/Login";
import NotFound from "@/views/NotFound";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        meta: {
            requiresAuth: true
        }
    },
    {
        path: '/board/:id(\\d+)',
        name: 'Board',
        component: Board,
        meta: {
            requiresAuth: true
        },
        children: [
            {
                path: 'list/:listId(\\d+)/card/:cardId(\\d+)',
                name: 'Card',
                component: Card
            }
        ]
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '*',
        name: 'NotFound',
        component: NotFound,
        meta: {
            requiresAuth: true
        },
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
store.commit('user/initUserInfo')
router.beforeEach((to, from, next) => {
    //如果该路由需要鉴权，则验证用户的信息,如果不通过跳登录页面
    if (to.matched.some(matched => matched.meta.requiresAuth) && !store.state.user.info) {
        next({name: 'Login'})
    } else {
        next()
    }
})
export default router
